package com.chanjiedata.changbs.module.login;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.help.PermissionHelper;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.chanjiedata.changbs.web.ui.WebViewActivity;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RegisterTypeActivity extends BaseActivity {

    private static final int REQUEST_CODE = 100;
    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.rv_type_one)
    RelativeLayout rvTypeOne;
    @BindView(R.id.rv_type_two)
    RelativeLayout rvTypeTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_type);
        StatusBarUtil.setRootViewFitsSystemWindows(this,false);
        StatusBarUtil.setTranslucentStatus(this);
        ButterKnife.bind(this);
        initListener();
    }

    private void initListener() {

        rvTypeOne.setOnClickListener(view -> {
            PermissionHelper.getInstance().getPermission(mContext, new PermissionHelper.PermissionCallBack() {
                @Override
                public void onSuccess() {
                    Intent intent = new Intent(mContext, CaptureActivity.class);
                    startActivityForResult(intent, REQUEST_CODE);
                }

                @Override
                public void onFail() {

                }
            }, Manifest.permission.CAMERA);
        });

        rvTypeTwo.setOnClickListener(view -> {

            startActivity(RegisterCodeActivity.class);
        });

        backTv.setOnClickListener(view -> {
            finish();
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
//                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                    WebViewActivity.startActivity(mContext, result + "&client=true");
//                    Intent intent = new Intent(mContext, RegisterActivity.class);
//                    intent.putExtra("inviter_id", result);
//                    startActivity(intent);
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(this, "扫描二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

}
